<?php
$conn = mysqli_connect("localhost:3306","root","","php");

// 开启事务(关闭自动提交)
mysqli_query($conn, "begin");

// 业务操作
// 一次性添加多个商品
$arr = ["香蕉","菠萝","香蕉","草莓","梨子"];// 存在同名的商品
$success = true;
foreach ($arr as $key => $name) {
    $sql = "insert into t_product (p_name) values ('$name')";
    $result = mysqli_query($conn, $sql);
    if(!$result){
        echo "有错误<br>";
        $success = false;
    }
}

// 是否执行成功
if($success){
    echo "添加成功<br>";
    // 提交事务
    mysqli_query($conn, "commit");
} else {
    echo "添加失败<br>";
    // 回滚事务
    mysqli_query($conn, "rollback");
}
mysqli_close($conn);
?>